﻿function ULS_SP() {
    if (ULS_SP.caller) {
        ULS_SP.caller.ULSTeamName = "Windows SharePoint Services 4";
        ULS_SP.caller.ULSFileName = "AIS.UI.RibbonGlue.js";
    }
}

// Initialization routines
Type.registerNamespace('AIS.UI.RibbonGlue');
AIS.UI.RibbonGlue.Component = function () {
    ULS_SP();
    AIS.UI.RibbonGlue.Component.initializeBase(this);
}

// Register the ribbon with the ribbon manager
AIS.UI.RibbonGlue.Component.initializePageComponent = function () {
    ULS_SP();
    var ribbonPageManager = SP.Ribbon.PageManager.get_instance();
    if (null !== ribbonPageManager) {
        if (null == AIS.UI.RibbonGlue.Component.instance) { alert('ERROR! AIS.UI.RibbonGlue.Component.instance is unexpectedly null.  Ribbon will be disabled'); }
        ribbonPageManager.addPageComponent(AIS.UI.RibbonGlue.Component.instance);
        ribbonPageManager.get_focusManager().requestFocusForComponent(AIS.UI.RibbonGlue.Component.instance);
    }
}

// Activation entry point for the glue.  
// Calls the registration code once the appropriate scripts have been loaded
AIS.UI.RibbonGlue.Component.activate = function () {
    ULS_SP();
    ExecuteOrDelayUntilScriptLoaded(Function.createDelegate(null, AIS.UI.RibbonGlue.Component.initializePageComponent), 'SP.Ribbon.js');
}

// Process a refresh of the ribbon
AIS.UI.RibbonGlue.Component.refreshRibbonStatus = function () {
    SP.Ribbon.PageManager.get_instance().get_commandDispatcher().executeCommand(Commands.CommandIds.ApplicationStateChanged, null);
}

// Glue
AIS.UI.RibbonGlue.Component.prototype = {
    getFocusedCommands: function () {
        ULS_SP();
        return [];
    },
    getGlobalCommands: function () {
        ULS_SP();
        return getGlobalCommands();
    },
    isFocusable: function () {
        ULS_SP();
        return true;
    },
    receiveFocus: function () {
        ULS_SP();
        return true;
    },
    yieldFocus: function () {
        ULS_SP();
        return true;
    },
    
    // Called for each ribbon command
    // If this returns a false, the control associated with the command will be disabled
    canHandleCommand: function (commandId) {
        ULS_SP();
        return commandEnabled(commandId);
    },
    handleCommand: function (commandId, properties, sequence) {
        ULS_SP();
        return handleCommand(commandId, properties, sequence);
    }
}

AIS.UI.RibbonGlue.Component.registerClass('AIS.UI.RibbonGlue.Component', CUI.Page.PageComponent);
AIS.UI.RibbonGlue.Component.instance = new AIS.UI.RibbonGlue.Component();

NotifyScriptLoadedAndExecuteWaitingJobs("AIS.UI.RibbonGlue.js");

// Self activation code
// NOTE: This script must be loaded *AFTER* all support ribbon scripts have been loaded for self activation to operate
function AISUIRibbonGlueActivator() {
    AIS.UI.RibbonGlue.Component.activate();
}
ExecuteOrDelayUntilScriptLoaded(AISUIRibbonGlueActivator, 'AIS.UI.RibbonGlue.js');